package org.inspursc.s0517.health.evaluation.controller;

import org.inspursc.s0517.health.common.util.ResponseUtil;
import org.inspursc.s0517.health.common.vo.ResponseVO;
import org.inspursc.s0517.health.evaluation.dao.OrganizationDao;
import org.inspursc.s0517.health.evaluation.dto.*;
import org.inspursc.s0517.health.evaluation.entity.InsSurveyAnswer;
import org.inspursc.s0517.health.evaluation.entity.Organization;
import org.inspursc.s0517.health.evaluation.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yuchu
 */
@RestController
@RequestMapping("share")
public class ReportShareController {

    @Autowired
    private ReportShareService reportShareService;

    @Autowired
    OrganizationDao organizationDao;

    @Autowired
    private InsSurveyAnswerService insSurveyAnswerService;

    @Autowired
    SurveyService surveyService;

    @GetMapping("/createReportShare")
    public ResponseEntity<ResponseVO> createReportShare(@RequestParam Integer reportId,
                                            @RequestParam Integer organizationId,
                                            HttpServletRequest request){

        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getServletContext().getContextPath()).append("/").toString();

        ReportShareDTO shareDTO = reportShareService.findByReportIdAndDateGreaterThanEqual(reportId,new Date());
        if(null != shareDTO){
            reportShareService.deleteById(shareDTO.getId());
        }

        ReportShareDTO dto = new ReportShareDTO();
        dto.setReportId(reportId);
        dto.setCustomerId(organizationId);
        try {
            ReportShareDTO reportShareDTO = reportShareService.saveReportShare(dto);
//            reportShareDTO.setUrl(tempContextUrl+"#/share?reportId="+reportShareDTO.getReportId());
            reportShareDTO.setUrl(tempContextUrl+"share#/share?reportId=" + reportShareDTO.getReportId());
            return ResponseUtil.success("分享成功", reportShareDTO);
        } catch (Exception e) {
            return ResponseUtil.error("分享失败");
        }
    }

    @PostMapping("share")
    public ResponseEntity<ResponseVO> share(@RequestParam Integer reportId,
                            @RequestParam String pass){

        ReportShareDTO reportShareDTO=reportShareService.findByReportIdAndPassAndDateGreaterThanEqual(reportId,pass,new Date());
        if(null!=reportShareDTO){
            Map<String, Object> dtoMap = new HashMap<>();
            InsSurveyAnswerGradeDTO insSurveyAnswerGradeDTO = insSurveyAnswerService.findOneById(reportId);
            Organization organization = organizationDao.findById(insSurveyAnswerGradeDTO.getOrganizationId()).get();  // reportShareDTO.getCustomerId();
            dtoMap.put("assessmentData", insSurveyAnswerGradeDTO);
            dtoMap.put("personnelData", organization);
            return ResponseUtil.success(dtoMap);
        }
        return ResponseUtil.error("分享密码不正确，不能查询分享!",false);
    }

    @GetMapping("getSurveyAnswerDetailByShare")
    public ResponseEntity<ResponseVO> getSurveyAnswerDetailByShare(
            @RequestParam Integer reportId,
            @RequestParam String pass,
            HttpServletRequest request){
        ReportShareDTO reportShareDTO = reportShareService.findByReportIdAndPassAndDateGreaterThanEqual(reportId,pass,new Date());
        if(null!=reportShareDTO){
            Map<String, Object> dtoMap = new HashMap<>();
            try {
                AssessmentAnswerDateilsDTO assessmentAnswerDateilsDTO = insSurveyAnswerService.getAnswerDetailById(reportId, request);
                SurveyQuDetailDTO surveyQuDetailDTO = surveyService.findOneSurveyDetail(assessmentAnswerDateilsDTO.getSurveyId());
                dtoMap.put("questionnaireData", surveyQuDetailDTO);
                dtoMap.put("answerData", assessmentAnswerDateilsDTO);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ResponseUtil.success("ok",dtoMap);
        }
        return ResponseUtil.error("分享密码不正确，不能查询分享!",false);
    }
}
